/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.confirmForm;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import utils.SaisieUtils;

/**
 *
 * @author Paul
 */
public class ConfirmerAssociationCours extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            this.doAny(request, response);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            this.doAny(request, response);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void doAny(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String[] cours = request.getParameterValues("cours");
        String[] etudiants = request.getParameterValues("etudiants");
        boolean isCours = SaisieUtils.isAllNotEmptyFields(cours);
        boolean isEtudiants = SaisieUtils.isAllNotEmptyFields(etudiants);
        if (isCours && isEtudiants) {
            //DAO
            request.setAttribute("message", "Les étudiants ont été ajouté aux cours.");
            request.getRequestDispatcher("view/associationCours.jsp").forward(request, response);
        } else if (!isCours) {
            request.setAttribute("message", "Veuillez sélectionner au moins un cours.");
            request.getRequestDispatcher("view/associationCours.jsp").forward(request, response);
        } else if (!isEtudiants) {
            request.setAttribute("message", "Veuillez sélectionner au moins un étudiant.");
            request.getRequestDispatcher("view/associationCours.jsp").forward(request, response);
        }
    }
}
